package com.jinlejia.erp.adapter;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.bean.EstateBean;
import com.jinlejia.erp.databinding.ItemSearchDistrictBinding;

import java.util.ArrayList;
import java.util.HashSet;


/**
 * 小区搜索结果Adapter
 * Created by Administrator on 2017/4/27 0027.
 */

public class SearchDistrictAdapter extends YfListAdapter {

    private String word;
    private String showBtn;
    private HashSet<String> selectedList = new HashSet();

    public SearchDistrictAdapter(ArrayList data, String keyWord, String showBtn) {
        super(data);
        this.word = keyWord;
        this.showBtn = showBtn;
    }

    public void setWord(String w) {
        this.word = w;
    }
    public void setSelectList( HashSet<String> selectList ){
        this.selectedList = selectList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_district, parent, false);
        //搜索结果
        return new ResultViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        String tmp = "<font color=\"#F0BA00\">" + word + "</font>";
        String str = ((EstateBean) mData.get(position)).getEstateName().replace(word, tmp);
        final ItemSearchDistrictBinding binding = DataBindingUtil.getBinding(holder.itemView);
        binding.tvHouseName.setText(Html.fromHtml(str));
        binding.tvHouseArea.setText(((EstateBean) mData.get(position)).getCounty() + " - " + (((EstateBean) mData.get(position)).getArea()));

        if( !TextUtils.isEmpty(showBtn) && showBtn.equals("CHECK")){
            binding.checkbox.setVisibility(View.VISIBLE);
            if( selectedList.contains(((EstateBean) mData.get(position)).getEstateName()) ){
                binding.checkbox.setChecked(true);
            }else{
                binding.checkbox.setChecked(false);
            }

        }

        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if( selectedList.contains(((EstateBean) mData.get(position)).getEstateName()) ){
                    binding.checkbox.setChecked(false);
                    selectedList.remove(((EstateBean) mData.get(position)).getEstateName());
                    if( null != rListiner ){
                        rListiner.OnRemove((EstateBean) mData.get(position));
                    }
                }else{
                    binding.checkbox.setChecked(true);
                    selectedList.add(((EstateBean) mData.get(position)).getEstateName());
                    if( listiner != null ){
                        listiner.OnSelected((EstateBean) mData.get(position));
                    }
                }

            }
        });

        binding.getRoot().setTag(mData.get(position));

    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_clear_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    static final class ResultViewHolder extends RecyclerView.ViewHolder {

        public ResultViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface OnSelectedListiner{
        void OnSelected(EstateBean icb);
    }
    private OnSelectedListiner listiner;
    public void setOnSelectedListiner( OnSelectedListiner l ){
        this.listiner = l;
    }

    public interface OnRemoveListiner{
        void OnRemove(EstateBean icb);
    }
    private OnRemoveListiner rListiner;
    public void setOnRemoveListiner( OnRemoveListiner l ){
        this.rListiner = l;
    }

}
